﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.FormatOutput;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormInvoiceDetailOfOrder : FormTemplet
    {

        private OrderCondition _orderCondition = null;

        private DataTable _dtDetail = null;

        private bool IsPressLeftButton = true;

        private FormNotifier f = null;

        private Point mousePoint = default(Point);

        public FormInvoiceDetailOfOrder()
        {
            this.InitializeComponent();
        }

        public FormInvoiceDetailOfOrder(OrderCondition oc) : this()
        {
            this._orderCondition = oc;
            Orders orders = new Orders();
            try
            {
                this._dtDetail = orders.GetInvoiceDetailOfOrder(this._orderCondition);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n订单信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            orders.Dispose();
            if (this._dtDetail != null)
            {
                if (this._dtDetail.Rows.Count > 0)
                {
                    this.dgDetail.DataSource = null;
                    this.dgDetail.DataSource = this._dtDetail;
                }
                else
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息\r\n\r\n订单项信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.dgDetail.DataSource = null;
                    base.Dispose();
                }
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.dgDetail.Rows[0].Cells[0].Value.ToString();
            }
            catch (Exception)
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int rowIndex = this.dgDetail.CurrentCell.RowIndex;
            new FormOrderInfoForInvoice(this.dgDetail.Rows[rowIndex].Cells[1].Value.ToString())
            {
                MdiParent = base.MdiParent
            }.Show();
        }

        private void dgDetail_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0 && e.RowIndex <= this.dgDetail.Rows.Count - 1)
            {
                new FormOrderInfoForInvoice(this.dgDetail.Rows[e.RowIndex].Cells[1].Value.ToString())
                {
                    MdiParent = base.MdiParent
                }.Show();
            }
        }

        private void FormInvoiceDetailOfOrder_Load(object sender, EventArgs e)
        {
            this.dgDetail.AutoResizeColumns();
        }

        private void btnStatistics_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dgDetail.Rows.Count; i++)
            {
                if (Convert.ToInt32(this.dgDetail.Rows[i].Cells["总数量"].Value) == Convert.ToInt32(this.dgDetail.Rows[i].Cells["已发货"].Value))
                {
                    this.dgDetail.Rows[i].DefaultCellStyle.BackColor = Color.LawnGreen;
                }
                if (Convert.ToInt32(this.dgDetail.Rows[i].Cells["总数量"].Value) < Convert.ToInt32(this.dgDetail.Rows[i].Cells["已发货"].Value))
                {
                    this.dgDetail.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                }
                if (Convert.ToInt32(this.dgDetail.Rows[i].Cells["总数量"].Value) > Convert.ToInt32(this.dgDetail.Rows[i].Cells["已发货"].Value) && Convert.ToInt32(this.dgDetail.Rows[i].Cells["已发货"].Value) > 0)
                {
                    this.dgDetail.Rows[i].DefaultCellStyle.BackColor = Color.Orange;
                }
            }
            if (this._dtDetail != null)
            {
                if (this._dtDetail.Rows.Count > 0)
                {
                    double num = 0.0;
                    double num2 = 0.0;
                    double num3 = 0.0;
                    double num4 = 0.0;
                    for (int i = 0; i < this._dtDetail.Rows.Count; i++)
                    {
                        num += (double)Convert.ToInt32(this._dtDetail.Rows[i]["总数量"]);
                        num3 += (double)Convert.ToInt32(this._dtDetail.Rows[i]["已发货"]);
                        num2 += Convert.ToDouble(this._dtDetail.Rows[i]["总面积"]);
                        num4 += Convert.ToDouble(this._dtDetail.Rows[i]["已发货面积"]);
                    }
                    MessageBox.Show(string.Concat(new string[]
                    {
                        "总数量为：",
                        num.ToString(),
                        "；已发货数量；",
                        num3.ToString(),
                        "\r\n总面积为：",
                        num2.ToString(),
                        "；已发货面积为：",
                        num4.ToString()
                    }), "统计", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息\r\n\r\n信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            this.saveFileDialog1.Filter = "Excel文件|*.xls";
            this.saveFileDialog1.Title = "请选择你要保存的路径和文件名";
            this.saveFileDialog1.FileName = "订单发货明细.xls";
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = this.saveFileDialog1.FileName;
                this.saveFileDialog1.Dispose();
                try
                {
                    ExcelOperationTypeSubtotal excelOperationTypeSubtotal = new ExcelOperationTypeSubtotal(Application.StartupPath);
                    excelOperationTypeSubtotal.MakeDataToExcel_2(fileName, new DataSet
                    {
                        Tables =
                        {
                            this._dtDetail.Copy()
                        }
                    });
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                this.saveFileDialog1.Dispose();
            }
        }

        private void dgDetail_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.f == null)
                {
                    this.f = new FormNotifier();
                }
                this.IsPressLeftButton = true;
                this.mousePoint.X = e.X;
                this.mousePoint.Y = e.Y;
                this.f.Left = Control.MousePosition.X + 17;
                this.f.Top = Control.MousePosition.Y;
                this.f.Show();
            }
        }

        private void dgDetail_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.f.Left = Control.MousePosition.X + 17;
                this.f.Top = Control.MousePosition.Y;
            }
        }

        private void dgDetail_MouseUp(object sender, MouseEventArgs e)
        {
            this.f.Hide();
        }

        private void dgDetail_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.IsPressLeftButton)
            {
                int num = 0;
                int num2 = 0;
                decimal d = 0m;
                decimal d2 = 0m;
                for (int i = 0; i < this.dgDetail.SelectedCells.Count; i++)
                {
                    if (this.dgDetail.SelectedCells[i].OwningColumn.HeaderText == "总数量")
                    {
                        num += Convert.ToInt32(this.dgDetail.SelectedCells[i].Value);
                    }
                    if (this.dgDetail.SelectedCells[i].OwningColumn.HeaderText == "已发货")
                    {
                        num2 += Convert.ToInt32(this.dgDetail.SelectedCells[i].Value);
                    }
                    else if (this.dgDetail.SelectedCells[i].OwningColumn.HeaderText == "总面积")
                    {
                        d += Convert.ToDecimal(this.dgDetail.SelectedCells[i].Value);
                    }
                    else if (this.dgDetail.SelectedCells[i].OwningColumn.HeaderText == "已发货面积")
                    {
                        d2 += Convert.ToDecimal(this.dgDetail.SelectedCells[i].Value);
                    }
                }
                if (this.f != null)
                {
                    this.f.lbl.Text = string.Concat(new string[]
                    {
                        "总数量/已发货：",
                        num.ToString(),
                        " / ",
                        num2.ToString(),
                        "\r\n总面积/已发货面积：",
                        d.ToString(),
                        " / ",
                        d2.ToString()
                    });
                }
            }
        }
    }
}
